package com.topscomm.pm.script;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.JSONObject;
import com.topscomm.cbo.pojo.CboApprovalNodeInsEntity;
import com.topscomm.pm.PmScript;
import com.topscomm.pm.pojo.PmInvoiceApplyEntity;
import com.topscomm.pm.pojo.PmReceiptApplyEntity;
import com.topscomm.pm.service.IPmInvoiceApplyService;
import com.topscomm.pm.service.IPmReceiptApplyService;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.SpringUtil;
import com.topscomm.tap.ITapService;
import com.topscomm.tap.TapEntity;
import com.topscomm.tap.common.ParameterCache;

import groovy.util.ScriptException;

/**
* @ClassName: ToaExpressAddressTestScript
* @Description: 调用物流系统前，参数合法性验证
* @author fanzhiming
* @date 2020年7月15日
* @modify:
 */
public class PmExpressAddressTestScript extends PmScript {
    
    /**
    * @Title: invoiceExpressAddressTest
    * @Description: 测试发票的物流参数是否合法
    * @author fanzm
    * @date  2020年7月16日 下午5:44:23
    * @param service
    * @param entity
    * @throws Exception
    * @throws
     */
    public void invoiceExpressAddressTest(ITapService service, TapEntity entity) throws Exception {
        // 审批节点实例
        CboApprovalNodeInsEntity nodeins = (CboApprovalNodeInsEntity) entity;
        String sourceid = ConvertUtil.convertToString(nodeins.getSourceid());
        // 来源单据类型
        String sourcetype = nodeins.getSourcetype();
        IPmInvoiceApplyService pmInvoiceApplyService = (IPmInvoiceApplyService) SpringUtil
                .getBean("pmInvoiceApplyService");        
        PmInvoiceApplyEntity pmInvoiceApplyEntity = pmInvoiceApplyService.queryById(sourceid);
        if("2".equals(pmInvoiceApplyEntity.getCategory()))return;//作废 不处理
        //listLine存子表行map
        List<Map<String, Object>> listLine = new ArrayList<>();       
        Map<String, Object> map = new HashMap<>(4);
        map.put("itemname", "发票");
        map.put("num", 1);
        map.put("description", "邮寄发票");
        listLine.add(map);
       
        //paramEntity存接口所需字段
        JSONObject paramMap = new JSONObject();
        paramMap.put("sourcedocno", pmInvoiceApplyEntity.getDocno());
        paramMap.put("sourcesys", "pm");
        paramMap.put("sourcemodule", sourcetype);
        paramMap.put("sourcemoduledoctype", sourcetype);
        paramMap.put("companycode", pmInvoiceApplyEntity.getInvoiceorgcode());
        paramMap.put("companyname", pmInvoiceApplyEntity.getInvoiceorgname());
        paramMap.put("deptcode", pmInvoiceApplyEntity.getDeptcode());
        paramMap.put("deptname", pmInvoiceApplyEntity.getDeptname());
        paramMap.put("usercode", pmInvoiceApplyEntity.getUsercode());
        paramMap.put("username", pmInvoiceApplyEntity.getUsername());
        paramMap.put("j_usercompany", pmInvoiceApplyEntity.getInvoiceorgname());//寄件
        paramMap.put("j_username",  ParameterCache.getValue("PmInvoice_Username_"+pmInvoiceApplyEntity.getInvoiceorgcode()));
        paramMap.put("j_usertel", ParameterCache.getValue("PmInvoice_Usertel_"+pmInvoiceApplyEntity.getInvoiceorgcode()));
        paramMap.put("j_usermobile", ParameterCache.getValue("PmInvoice_Usertel_"+pmInvoiceApplyEntity.getInvoiceorgcode()));
        paramMap.put("j_useraddress", ParameterCache.getValue("PmInvoice_Address_"+pmInvoiceApplyEntity.getInvoiceorgcode()));
        
        paramMap.put("d_usercompany", "");//收件
        paramMap.put("d_username", pmInvoiceApplyEntity.getReceiver());
        paramMap.put("d_usertel", pmInvoiceApplyEntity.getReceivertel());
        paramMap.put("d_usermobile", pmInvoiceApplyEntity.getReceivertel());
        paramMap.put("d_useraddress", pmInvoiceApplyEntity.getMailaddress());        
        String express = ParameterCache.getValue("PmExpress_express");
        String pay_method = ParameterCache.getValue("PmExpress_pay_method");
        paramMap.put("express", express);//必填，1：顺丰 2德邦
        paramMap.put("pay_method",pay_method);//0寄付 1到付
        paramMap.put("listLine", listLine);        
        // 调用物流接口
        String pmExpressOrderUrl = ParameterCache.getValue("PmExpressAddressTestUrl");
        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        requestFactory.setConnectTimeout(2000);// 设置超时
        requestFactory.setReadTimeout(10000);//读取超时时间
        RestTemplate restTemplate = new RestTemplate(requestFactory);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);        
        HttpEntity<JSONObject> request = new HttpEntity<>(paramMap, headers);       
        try {           
            ResponseEntity<JSONObject> response = restTemplate
                .postForEntity(pmExpressOrderUrl, request, JSONObject.class);
            //如果失败
           if (!response.getBody().getBoolean("success")) {
                throw new ScriptException(response.getBody().getString("msg"));
            }
        }
        catch(Exception e)
        {               
            throw new ScriptException(e.getMessage());
        }
    }
    
    /**
     * @Title: receiptExpressOrder
     * @Description: 收据调用物流参数验证
     * @param @param service
     * @param @param entity  参数
     * @return void    返回类型
     * @throws Exception 
     * @throws
     * @author fanzm
     */
    public void receiptExpressAddressTest(ITapService service, TapEntity entity) throws Exception {
        // 审批节点实例
        CboApprovalNodeInsEntity nodeins = (CboApprovalNodeInsEntity) entity;
        String sourceid = ConvertUtil.convertToString(nodeins.getSourceid());
        // 来源单据类型
        String sourcetype = nodeins.getSourcetype();
        IPmReceiptApplyService pmReceiptApplyService = (IPmReceiptApplyService) SpringUtil
                .getBean("pmReceiptApplyService");        
        PmReceiptApplyEntity pmReceiptApplyEntity = pmReceiptApplyService.queryById(sourceid);
        if("2".equals(pmReceiptApplyEntity.getCategory()))return;//作废的业务类型 不处理
        //listLine存子表行map
        List<Map<String, Object>> listLine = new ArrayList<>();       
        Map<String, Object> map = new HashMap<>(4);
        map.put("itemname", "收据单号:"+pmReceiptApplyEntity.getDocno());
        map.put("num", 1);
        map.put("description", "工程项目管理系统，邮寄收据");
        listLine.add(map);
       
        //paramEntity存接口所需字段
        JSONObject paramMap = new JSONObject();
        paramMap.put("sourcedocno", pmReceiptApplyEntity.getDocno());
        paramMap.put("sourcesys", "pm");
        paramMap.put("sourcemodule", sourcetype);
        paramMap.put("sourcemoduledoctype", sourcetype);
        paramMap.put("companycode", pmReceiptApplyEntity.getReceiptorgcode());
        paramMap.put("companyname", pmReceiptApplyEntity.getReceiptorgname());
        paramMap.put("deptcode", pmReceiptApplyEntity.getDeptcode());
        paramMap.put("deptname", pmReceiptApplyEntity.getDeptname());
        paramMap.put("usercode", pmReceiptApplyEntity.getUsercode());
        paramMap.put("username", pmReceiptApplyEntity.getUsername());
        paramMap.put("j_usercompany", pmReceiptApplyEntity.getReceiptorgname());//寄件
        paramMap.put("j_username",  ParameterCache.getValue("PmInvoice_Username_"+pmReceiptApplyEntity.getReceiptorgcode()));
        paramMap.put("j_usertel", ParameterCache.getValue("PmInvoice_Usertel_"+pmReceiptApplyEntity.getReceiptorgcode()));
        paramMap.put("j_usermobile", ParameterCache.getValue("PmInvoice_Usertel_"+pmReceiptApplyEntity.getReceiptorgcode()));
        paramMap.put("j_useraddress", ParameterCache.getValue("PmInvoice_Address_"+pmReceiptApplyEntity.getReceiptorgcode()));
        
        paramMap.put("d_usercompany", "");//收件
        paramMap.put("d_username", pmReceiptApplyEntity.getReceiver());
        paramMap.put("d_usertel", pmReceiptApplyEntity.getReceivertel());
        paramMap.put("d_usermobile", pmReceiptApplyEntity.getReceivertel());
        paramMap.put("d_useraddress", pmReceiptApplyEntity.getMailaddress());
        String express = ParameterCache.getValue("PmExpress_express");
        String pay_method = ParameterCache.getValue("PmExpress_pay_method");
        paramMap.put("express", express);//必填，1：顺丰 2德邦
        paramMap.put("pay_method",pay_method);//0寄付 1到付
        paramMap.put("listLine", listLine);        
        // 调用物流接口
        String pmExpressOrderUrl = ParameterCache.getValue("PmExpressAddressTestUrl");
        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        requestFactory.setConnectTimeout(2000);// 设置超时
        requestFactory.setReadTimeout(10000);//读取超时时间
        RestTemplate restTemplate = new RestTemplate(requestFactory);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);        
        HttpEntity<JSONObject> request = new HttpEntity<>(paramMap, headers);       
        try {           
            ResponseEntity<JSONObject> response = restTemplate
                .postForEntity(pmExpressOrderUrl, request, JSONObject.class);
            //如果失败
           if (!response.getBody().getBoolean("success")) {
                throw new ScriptException(response.getBody().getString("msg"));
            }
        }
        catch(Exception e)
        {               
            throw new ScriptException(e.getMessage());
        }
    }
}
